package com.campsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.QueryPageParam;
import com.campsys.common.Result;
import com.campsys.entity.Class;
import com.campsys.entity.Course;
import com.campsys.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping({"/course"})
public class CourseController {
    @Autowired
    private CourseService courseService;
    @PostMapping("/list")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        Page<Course> page =new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = (String) param.get("name");
        if (StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(Course::getCourseName,name);
        }
        IPage result =courseService.page(page,lambdaQueryWrapper);
        return Result.suc(result.getTotal(),result.getRecords());
    }

    @PostMapping("/save")
    public Result save(@RequestBody Course course){
        return courseService.save(course)?Result.suc():Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Course course){
        return courseService.updateById(course)?Result.suc():Result.fail();
    }
    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return courseService.removeById(id)?Result.suc():Result.fail();
    }
//    @GetMapping("/getCourses")
//    public List<Course> getCourses(){
//        return courseService.getCourses();
//    }
}
